package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.model.RecordRemarks;
import com.cn.jdia.tas.service.IRecordRemarksService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/control")
public class RecordRemarkController {

	@Resource
	private IRecordRemarksService rcdRemarkService = null;

	@RequestMapping("/addrecordremark")
	public String addRecordRemark(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");
		Gson gson = new Gson();
		Map<String, String> pmap = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		System.out.println(data);
		String pid = pmap.get("pid");
		String date = pmap.get("date");
		String type = pmap.get("type");
		String sysloc = pmap.get("sysloc");
		String inputloc = pmap.get("inputloc");
		String remark = pmap.get("remark");

		RecordRemarks rcdRemark = new RecordRemarks();
		rcdRemark.setEmployeeid(pid);
		rcdRemark.setCheckdate(date);
		rcdRemark.setChecktype(type);
		rcdRemark.setSysloc(sysloc);
		rcdRemark.setInputloc(inputloc);
		rcdRemark.setRemark(remark);

		int ret = rcdRemarkService.insertRecordRemark(rcdRemark);

		out.write(String.valueOf(ret));
		out.flush();
		out.close();
		return null;
	}

	@RequestMapping("/updaterecordremark")
	public String updateRecordRemark(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");
		Gson gson = new Gson();
		Map<String, String> pmap = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		System.out.println(data);
		String pid = pmap.get("pid");
		String date = pmap.get("date");
		String type = pmap.get("type");
		// String sysloc = pmap.get("sysloc");
		String inputloc = pmap.get("inputloc");
		String remark = pmap.get("remark");

		int ret = rcdRemarkService.updateRecordRemarks(pid, date, type, inputloc, remark);

		out.write(String.valueOf(ret));
		out.flush();
		out.close();
		return null;
	}

	@RequestMapping("/getrecordremark")
	public String getRecordRemark(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");
		Gson gson = new Gson();
		Map<String, String> pmap = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		System.out.println(data);
		String pid = pmap.get("pid");
		String date = pmap.get("date");
		String type = pmap.get("type");

		Map<String, String> rmap = new HashMap<>();

		RecordRemarks recordRemarks = rcdRemarkService.getRecordRemarks(pid, date, type);
		if (recordRemarks == null) {
			System.out.println("NULL");
			rmap.put("num", "0");
		} else {
			rmap.put("num", "1");
			rmap.put("sysloc", recordRemarks.getSysloc());
			rmap.put("inloc", recordRemarks.getInputloc());
			rmap.put("remark", recordRemarks.getRemark());
		}

		out.write(gson.toJson(rmap));
		out.flush();
		out.close();
		return null;
	}
	
	@RequestMapping("/get7daysrecordremark")
	public String get7DaysRecordRemark(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");
		Gson gson = new Gson();
		Map<String, String> pmap = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		System.out.println(data);
		String pid = pmap.get("id");

		DateTime nowDate = new DateTime();
		String end=nowDate.toString("yyyy-MM-dd");
		String start=nowDate.minusDays(7).toString("yyyy-MM-dd");
		
		System.out.println(pid+" "+start+"   "+end);
		List<RecordRemarks> sevenDaysRemarks = rcdRemarkService.get7DaysRemarks(pid, start, end);
		List<Map<String,String>> rlist=new ArrayList<>();
		Map<String,String> map=null;
		for(RecordRemarks mark:sevenDaysRemarks){
			map=new HashMap<>();
			map.put("date", mark.getCheckdate());
			map.put("mark", mark.getRemark());
			map.put("inloc", mark.getInputloc());
			map.put("type", mark.getChecktype());
			rlist.add(map);
		}
		
		out.write(gson.toJson(rlist));
		out.flush();
		out.close();
		return null;
	}
}
